<template>
  <div class="comment">
    <div class="commentmian" v-for="(item,index) in commentData" :key="index">
      <div class="user">
        <img :src="'http://157.122.54.189:9095'+item.account.defaultAvatar" alt />
        <span>{{item.account.nickname}}</span>
        <span>{{item.account.created_at | formatDate}}</span>
        <i>{{item.level}}</i>
      </div>
      <div class="parentComment" v-if="item.parent">
        <ParentComment :parentData="item.parent" @parentCallReply="parentCallReply" />
      </div>
      <div class="mianContent">
        <div class="mian_pic" v-if="item.pics.length">
          <img :src="$axios.defaults.baseURL+item.pics[0].url" alt />
        </div>
        {{item.content}}
      </div>
      <div class="callreplay">
        <a href="JavaScript:;" @click="callReplay(item.id,item.account.nickname)">回复</a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["commentData", "total"],
  data() {
    return {};
  },
  created() {
    console.log(this.commentData);
  },
  filters: {
    formatDate: function (value) {
      // 时间戳转换日期格式方法
      if (value == null) {
        return "";
      } else {
        let date = new Date(value);
        let y = date.getFullYear(); // 年
        let MM = date.getMonth() + 1; // 月
        MM = MM < 10 ? "0" + MM : MM;
        let d = date.getDate(); // 日
        d = d < 10 ? "0" + d : d;
        let h = date.getHours(); // 时
        h = h < 10 ? "0" + h : h;
        let m = date.getMinutes(); // 分
        m = m < 10 ? "0" + m : m;
        let s = date.getSeconds(); // 秒
        s = s < 10 ? "0" + s : s;
        return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
      }
    },
  },
  methods: {
    callReplay(id, nickname) {
      // 这里是主评论的回复被点击了,
      // 应该通知父页面, 并且把自己的 id 传出去,准备用作 parentId
      this.$emit("callReply", id, nickname);
      // console.log(id);
      // console.log(nickname);
    },
    // 现在是主评论的组件, 接收到了 parent 父评论传出来 的parentId
    // callReply 事件是用来通知父页面需要发起回复的
    parentCallReply(parentId, nikename) {
      this.$emit("callReply", parentId, nikename);
    },
  },
};
</script>

<style lang="less" scoped>
.commentmian {
  border-bottom: 1px dashed #ddd;
  padding: 20px 20px 5px;
  .user {
    margin-bottom: 10px;
    font-size: 12px;
    color: #666;
    img {
      width: 16px;
      height: 16px;
      border-radius: 50%;
    }
    span {
      vertical-align: top;
    }
    i {
      float: right;
      vertical-align: top;
    }
  }
  .mianContent {
    margin-top: 10px;
    .mian_pic {
      width: 80px;
      height: 80px;
      border-radius: 6px;
      overflow: hidden;
      margin-right: 5px;
      margin-top: 10px;
      padding: 5px;
      border: 1px dashed #ddd;
      img {
        display: block;
        width: 100%;
        height: 100%;
        object-fit: cover;
        cursor: pointer;
      }
    }
  }
  .callreplay {
    text-align: right;
    line-height: 20px;
    font-size: 12px;
    height: 20px;
    &:hover a {
      display: block;
    }
    a {
      display: none;
      color: #1e50a2;
      cursor: pointer;
    }
  }
}
</style>